package henu.cn.androidhomework.Login;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;

import cn.bmob.v3.listener.UpdateListener;
import henu.cn.androidhomework.Main2Activity;
import henu.cn.androidhomework.MainActivity;
import henu.cn.androidhomework.R;
import cn.bmob.v3.Bmob;

public class registeredUser extends AppCompatActivity {
    private Button Regitster;
    private EditText userName;
    private EditText userPhone;
    private EditText userPassword;
    private EditText userIdentifyCode;
    private TextView xieyi;
    private Button btnRegister;
    private String IdentifyCode;
    private boolean IsTrue = false;

    @SuppressLint("ResourceAsColor")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered_user);
        Regitster = (Button) findViewById(R.id.Register);
        btnRegister = (Button) findViewById(R.id.btnRegister);
        userName = (EditText) findViewById(R.id.userName);
        userPhone = (EditText) findViewById(R.id.userPhone);
        userPassword = (EditText) findViewById(R.id.userPassword);
        userIdentifyCode = (EditText) findViewById(R.id.userIdentifyCode);
        xieyi = (TextView) findViewById(R.id.xieyi);
        Bmob.initialize(this, "f37ce38de3435d43b37d223c1d7cfd6d");

        /**
         * 在代码中动态改变drawable中的资源文件(这里指的是button的属性)
         */
        GradientDrawable gd = new GradientDrawable();//创建drawabl
        gd.setGradientType(GradientDrawable.RECTANGLE);
        gd.setColor(R.color.Green_纯绿);
        gd.setCornerRadius(30);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            Regitster.setBackground(gd);
        }

        /**
         * 对元素进行初始化操作
         */


        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        /**
                         * 类 BmobSMS的方法讲解
                         * http://docs.bmob.cn/data/Android/i_doc/doc/cn/bmob/v3/BmobSMS.html
                         */
                        if(userPhone.getText().length()==11 && userPhone.getText().length()!=0){
                            BmobSMS.requestSMSCode(userPhone.getText().toString(), "寒山远上", new QueryListener<Integer>() {
                                @Override
                                public void done(Integer smsId, BmobException e) {
                                    if (e == null) {//验证码发送成功
                                        IdentifyCode = smsId.toString();
                                        showToast("bmob" + "短信id：" + smsId.toString()+"=="+IdentifyCode);//用于查询本次短信发送详情
                                    } else {
                                        showToast("发送验证码失败:" + e.toString());
                                    }
                                }
                            });
                        }else {
                            showToast("手机号不能为空且只能输入11位数字！");
                            userPhone.setText("");
                        }
                    }
                }).start();
            }
        });


        Regitster.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // public static rx.Subscription verifySmsCode(java.lang.String phoneNumber,
//                        java.lang.String smsCode,
//                        UpdateListener listener)
//
//                验证验证码
//                参数:
//                phoneNumber：手机号码 -
//                smsCode - ：短信验证码
//                listener - ：监听回调

                BmobSMS.verifySmsCode(userPhone.getText().toString(), userIdentifyCode.getText().toString(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                           showToast("验证码验证成功，您可以在此时进行绑定操作！\n");
                            IsTrue = true;
                            new Thread(new Runnable() {
                                @Override
                                public void run() {


                                    DB db = new DB();
                                    if(userName.getText().toString()!=null){
                                        db.setName(userName.getText().toString());
                                    }else {
                                        showToast("用户名不能为空！");
                                    }
                                    if(userPassword.getText().length()>=6){
                                        db.setPassword(userPassword.getText().toString());
                                    }else {
                                        showToast("密码不得少于6位，请重新输入!");
                                        userPassword.setText("");
                                    }

                                    db.setPhonenumber(userPhone.getText().toString());

                                    db.save(new SaveListener<String>() {
                                        @Override
                                        public void done(String s, BmobException e) {
                                            if (e == null) {
                                                showToast("数据上传成功,id是:" + s.toString());
//                                                Intent intent=new Intent(registeredUser.this,LoginActivity.class);
//                                                intent.putExtra("id",s.toString());
//                                                //sendBroadcast(intent);
//                                                startActivity(intent);
                                            } else {
                                                showToast("数据上传失败:" + e.toString());
                                            }
                                        }
                                    });

                                }
                            }).start();
                        } else {
                           showToast("验证码验证失败：" + e.getErrorCode() + "-" + e.getMessage() + "\n");
                        }
                    }
                });
            }
        });

        xieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(registeredUser.this, Main2Activity.class);
                startActivity(intent);
            }
        });
    }

    public void showToast(String msg) {
        Toast.makeText(registeredUser.this, msg, Toast.LENGTH_LONG).show();
    }
}


//                        DB db=new DB();
//                        db.setName("hanshan");
//                        db.save(new SaveListener<String>() {
//                            @Override
//                            public void done(String s, BmobException e) {
//                                if(e==null){
//                                    showToast("上传成功"+s.toString());
//                                }else{
//                                    showToast("上传失败"+e.toString());
//                                }
//                            }
//                        });
//                        DBUserInformation dbUserInformation = new DBUserInformation();
//                       // dbUserInformation.setUsername(userName.getText().toString());
//                       // dbUserInformation.setPhonenumber(userPhone.getText().toString());
//                       // dbUserInformation.setPassword(userPassword.getText().toString());
//                      //  dbUserInformation.setID(userIdentifyCode.getText().toString());
//dbUserInformation.setUsername("hanshan");
//dbUserInformation.save(new SaveListener<String>() {
//    @Override
//
//    public void done(String objectId, BmobException e) {
//        if (e == null) {
//            Toast.makeText(registeredUser.this, "添加数据成功，返回objectId为：" + objectId, Toast.LENGTH_LONG).show();
//        } else {
//            Toast.makeText(registeredUser.this, "创建数据失败：" + e.getMessage(), Toast.LENGTH_LONG).show();
//
//        }
//    }
//});
//                        dbUserInformation.save(new SaveListener<String>() {
//                            @Override
//                            public void done(String s, BmobException e) {
//                                if (e == null) {
//                                    showToast("添加数据成功，返回objectId为：" + s);
//                                } else {
//                                    showToast("创建数据失败：" + e.getMessage());
//                                }
//                            }
//                        });

//        }
//    }).start();

//showToast(userName.getText().toString()+userIdentifyCode.getText().toString());
//        Person p2 = new Person();
//        p2.setName("杨校庆");
//        p2.setAddress("河南开封");
//        p2.save(new SaveListener<String>() {
//            @Override
//            public void done(String objectId, BmobException e) {
//                if (e == null) {
//                    Toast.makeText(MainActivity.this, "添加数据成功，返回objectId为：" + objectId, Toast.LENGTH_LONG).show();
//                } else {
//                    Toast.makeText(MainActivity.this,"创建数据失败：" + e.getMessage(),Toast.LENGTH_LONG).show();
//
//                }
//            }
//        });

//                DBUserInformation dbUserInformation=new DBUserInformation();
//                dbUserInformation.setUsername(userName.getText().toString());
//                dbUserInformation.setPhonenumber(userPhone.getText().toString());
//                dbUserInformation.setPassword(userPassword.getText().toString());
//                dbUserInformation.setID(userIdentifyCode.getText().toString());
//
//                dbUserInformation.save(new SaveListener<String>() {
//                    @Override
//                    public void done(String s, BmobException e) {
//                        if(e==null){
//                            showToast("添加数据成功，返回objectId为："+s);
//                        }else{
//                            showToast("创建数据失败：" + e.getMessage());
//                        }
//                    }
//                });
//            }
//
//        });
//
//        btnRegister.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//
//
//
//

